//
//  LoginViewController.m
//  qiuShiJiangHu
//
//  Created by xalo on 16/1/25.
//  Copyright © 2016年 糗事江湖项目组. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistViewController.h"
#import "TabBarViewController.h"
#import "LoginHelper.h"
@interface LoginViewController ()<UITextFieldDelegate>


@property (strong, nonatomic) IBOutlet UITextField *userNameTF;


@property (strong, nonatomic) IBOutlet UITextField *passWordTF;

@property (strong, nonatomic) IBOutlet UIButton *loginWithSinaBlog;

@property (strong, nonatomic) IBOutlet UIButton *loginWithRenRen;

@property (strong, nonatomic) IBOutlet UIButton *regist;

@property (strong, nonatomic) IBOutlet UIButton *login;

@property (strong, nonatomic) IBOutlet UIImageView *sinaPicture;

@property (strong, nonatomic) IBOutlet UIImageView *renRenPicture;




@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self keyboardReturn];
    [self setLoginViewBasicInfo];
    
    
    
    // Do any additional setup after loading the view.
}

- (void)setLoginViewBasicInfo {
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"loginBack.jpg"]];
    self.loginWithSinaBlog.backgroundColor = [UIColor whiteColor];
    self.sinaPicture.image = [UIImage imageNamed:@"sinaBlog.png"];
    self.loginWithRenRen.backgroundColor = [UIColor whiteColor];
    self.renRenPicture.image = [UIImage imageNamed:@"renren.jpg"];
    
    [self.login.layer setMasksToBounds:YES];
    [self.login.layer setCornerRadius:10.0];
    self.login.backgroundColor = [UIColor orangeColor];
    
    [self.regist.layer setMasksToBounds:YES];
    [self.regist.layer setCornerRadius:10.0];
    self.regist.backgroundColor = [UIColor orangeColor];
}



- (void)keyboardReturn {
    
    self.userNameTF.delegate = self;
    self.passWordTF.delegate = self;

}


- (void)turnToRegistView {
    
    RegistViewController *registVC = [[UIStoryboard storyboardWithName:@"yan" bundle:nil]instantiateViewControllerWithIdentifier:@"RegistViewController"];
    [self presentViewController:registVC animated:YES completion:nil];
    
    
}




- (IBAction)registButton:(UIButton *)sender {
    
    //登录界面的注册按钮
    
    [self turnToRegistView];
    
    
}



- (IBAction)loginButton:(UIButton *)sender {
    
    
    [self judgeLoginAction];
    
    
}

- (void)judgeLoginAction {
    
    //登录按钮
    if ([self.userNameTF.text isEqualToString:@""] || [self.passWordTF.text isEqualToString:@""]) {
        [self setAlertWithTitle:@"温馨提示" message:@"用户名或密码不能为空"];
    }else {
        
        NSUserDefaults *userInfo = [NSUserDefaults standardUserDefaults];
        NSString *userName = [userInfo valueForKey:@"userName"];
        NSString *passWord = [userInfo valueForKey:@"passWord"];
        
        if ([self.userNameTF.text isEqualToString:userName] && [self.passWordTF.text isEqualToString:passWord]) {
            
            LoginHelper *loginH = [LoginHelper sharedManager];
           
            loginH.isLogin = YES;
            
            __weak LoginViewController *weakSelf = self;
            [self dismissViewControllerAnimated:YES completion:^{
                
                weakSelf.block();
                
            }];
            
        }else{
            //
            
            [self setAlertWithTitle:@"温馨提示" message:@"用户名或密码输入错误"];
        }
        
    }

}








- (void)setAlertWithTitle:(NSString *)title message:(NSString *)message {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
    alert.view.tintColor = [UIColor orangeColor];
    [alert addAction:okAction];
    [self presentViewController:alert animated:YES completion:nil];
    
}








- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    
    [textField resignFirstResponder];
    return YES;
    
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
